package com.cuitrip.business.tripservice.ui;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cuitrip.business.user.proxy.UserInfoProxy;
import com.cuitrip.service.R;
import com.lab.component.common.BaseDialogFragment;
import com.lab.logtrack.AppLog;
import com.lab.widget.WordCountView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ak
 */
public class EditTextDialog extends BaseDialogFragment {


    /* @Bind(R.id.hide_keyboard)
     TextView mHideKeyboard;*/
    @Bind(R.id.image_description)
    WordCountView mImageDescription;
    private View mRootView;

    private int mDialogWidthPixels;
    private int mDialogHeightPixels;

    private EditTextCallback mEditTextCallback;


    public static EditTextDialog getInstance() {
        EditTextDialog choiceDialog = new EditTextDialog();
        return choiceDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHideWhenStop(false);
        setCancelable(isCancelable());
        if (UserInfoProxy.getInstance().isTraveller()) {
            setStyle(DialogFragment.STYLE_NO_TITLE, R.style.TravellerChoiceDialog);
        } else {
            setStyle(DialogFragment.STYLE_NO_TITLE, R.style.FinderChoiceDialog);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.ct_layout_edit_dialog, null, false);
        ButterKnife.bind(this, mRootView);
        return mRootView;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        return dialog;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        try {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            int defaultDialagWidth = getDialog().getWindow().getAttributes().width;
            int defaultDialogHeight = getDialog().getWindow().getAttributes().height;
            int specifyDialogWidth = defaultDialagWidth;
            int specifyDialogHeight = defaultDialogHeight;
            if (mDialogWidthPixels < 0) {
                specifyDialogWidth = dm.widthPixels;
            } else if (mDialogWidthPixels > 0) {
                specifyDialogWidth = mDialogWidthPixels;
            } else {
                specifyDialogWidth = defaultDialagWidth;
            }
            if (mDialogHeightPixels < 0) {
                specifyDialogHeight = dm.heightPixels;
            } else if (mDialogHeightPixels > 0) {
                specifyDialogHeight = mDialogHeightPixels;
            } else {
                specifyDialogHeight = defaultDialogHeight;
            }
            getDialog().getWindow().setLayout(specifyDialogWidth, specifyDialogHeight);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /*@OnClick({R.id.hide_keyboard})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.hide_keyboard:
                if (mEditTextCallback != null){
                    String text = mImageDescription.getContent();
                    mEditTextCallback.onPositiveTextCallBack(text);
                }
                hideDialogFragment(this);
                break;
        }
    }*/

    public EditTextCallback getEditTextCallback() {
        return mEditTextCallback;
    }

    public void setEditTextCallback(EditTextCallback mEditTextCallback) {
        this.mEditTextCallback = mEditTextCallback;
    }

    public interface EditTextCallback {

        boolean onPositiveTextCallBack(String text);
    }
}
